<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-claim">&nbsp;&nbsp;采购管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;供应商添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="suppliesForm"
            :rules="rules"
            ref="suppliesForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="供应商:" prop="name">
            <el-input v-model="suppliesForm.name" placeholder="输入供应商"></el-input>
          </el-form-item>
          <el-form-item label="企业地址:" prop="address">
            <el-input v-model="suppliesForm.address" placeholder="输入企业地址"></el-input>
          </el-form-item>
          <el-form-item label="联系人:" prop="contract">
            <el-input v-model="suppliesForm.contract" placeholder="请输入联系人"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="电子邮件:" prop="email">
                <el-input v-model="suppliesForm.email" placeholder="请输入电子邮件"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话:" prop="cell">
                <el-input v-model="suppliesForm.cell" placeholder="请输入电话"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品类型:" prop="categoryType">
                <el-input v-model="suppliesForm.categoryType" placeholder="请输入商品类型"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运输方式:" prop="transport">
                <el-input v-model="suppliesForm.transport" placeholder="请输入运输方式"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="费用:" prop="cost">
            <el-input v-model="suppliesForm.cost" placeholder="请输入费用"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('suppliesForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('suppliesForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      suppliesForm: {
        name: '联想',
        address: '天津和平区',
        contract: '阿金',
        cell: '17677351987',
        email: "15437@qq.com",
        categoryType: "数码产品",
        transport: '货运',
        cost: 23000,
      },
      rules: {
        name: [
          {required: true, message: "请输入供应商", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        address: [
          {required: true, message: "请输入企业地址", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        cell: [
          {required: true, message: "请输入电话", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        contract: [
          {required: true, message: "请输入联系人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        email: [
          {required: true, message: "请输入电子邮件", trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: "请输入中文", trigger: 'blur'}
        ],
        categoryType: [
          {required: true, message: "请输入商品类型", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        transport: [
          {required: true, message: "请输入运输方式", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        cost: [
          {required: true, message: "请输入费用", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/purchaseSuppliers/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.suppliesForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>